/*
 * Copyright 2011 Christoph Ludwig (christoph[at]chludwig.de). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.chludwig.ehumanities.provenance.utils;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Computes file system paths based on the application's data folder.
 */
public class FileUtil {

  static private final FileFilter FILE_FILTER = new FileFilter() {

    @Override
    public boolean accept(File pathname) {
      return pathname.isFile();
    }

  };

  static private final FileFilter DIRECTORY_FILTER = new FileFilter() {

    @Override
    public boolean accept(File pathname) {
      return pathname.isDirectory();
    }

  };

  
  public static boolean treeModifiedSince(File folder, long cutoffMillisSinceEpoch) {
    if (folder.lastModified() > cutoffMillisSinceEpoch) {
      return true;
    }
    for (File containedFile : getAllFiles(folder)) {
      if (containedFile.lastModified() > cutoffMillisSinceEpoch) {
        return true;
      }
    }
    for (File containedFolder : getAllDirectories(folder)) {
      if (treeModifiedSince(containedFolder, cutoffMillisSinceEpoch)) {
        return true;
      }
    }
    return false;
  }

  public static boolean deleteRecursively(File fileOrFolder) {
    for (File containedFolder : getAllDirectories(fileOrFolder)) {
      if (!deleteRecursively(containedFolder)) {
        return false;
      }
    }
    for (File containedFile : getAllFiles(fileOrFolder)) {
      if (!containedFile.delete()) {
        return false;
      }
    }
    return fileOrFolder.delete();
  }

  /**
   * Lists all subdirectories contained in sourceFile.
   * 
   * If sourceFile is not a directory, then the result is an empty list.
   * 
   * @param fileOrFolder
   *          The file or directory whose elements are to be listed.
   * @return a list of all direct subdirectories contained in sourceFile.
   */
  public static List<File> getAllDirectories(File fileOrFolder) {
    File[] files = fileOrFolder.listFiles(DIRECTORY_FILTER);
    List<File> result = (files == null) ? Collections.<File> emptyList() : Arrays.asList(files);
    return result;
  }

  /**
   * Lists all regular files contained in sourceFile or its argument if it is
   * already a regular file.
   * 
   * @param fileOrFolder
   *          The file or directory whose elements are to be listed.
   * @return a list of all regular files directly in sourceFile if it is a
   *         directory or a singleton list containing only sourceFile else.
   */
  public static List<File> getAllFiles(File fileOrFolder) {
    File[] files = fileOrFolder.listFiles(FILE_FILTER);
    List<File> result = (files == null) ? Collections.singletonList(fileOrFolder) : Arrays.asList(files);
    return result;
  }

}
